#!/usr/bin/env bash

# Enable trace printing and exit on the first error
set -ex

get_config_value="/vagrant/scripts/get_config_value.sh"
use_nfs_for_synced_folders=$(bash ${get_config_value} "guest_use_nfs")
magento_host_name=$(bash ${get_config_value} "magento_host_name")

declare -A setupOptions
setupOptions[admin_frontname]=$(bash ${get_config_value} "magento_admin_frontname")
setupOptions[language]=$(bash ${get_config_value} "magento_language")
setupOptions[timezone]=$(bash ${get_config_value} "magento_timezone")
setupOptions[currency]=$(bash ${get_config_value} "magento_currency")
setupOptions[admin_user]=$(bash ${get_config_value} "magento_admin_user")
setupOptions[admin_password]=$(bash ${get_config_value} "magento_admin_password")
setupOptions[db_host]='localhost'
setupOptions[db_name]='magento'
setupOptions[db_user]='root'
setupOptions[base_url]="http://${magento_host_name}/"
setupOptions[admin_lastname]='Admin'
setupOptions[admin_firstname]='Admin'
setupOptions[admin_email]='admin@example.com'
setupOptions[amqp_host]='localhost'
setupOptions[amqp_port]='5672'
setupOptions[amqp_user]='guest'
setupOptions[amqp_password]='guest'

cd ${MAGENTO_ROOT}

# Clear cache
m-clear-cache

# Remove configuration files
rm -f "${MAGENTO_ROOT}/app/etc/config.php"
rm -f "${MAGENTO_ROOT}/app/etc/env.php"

# Create DB
db_names=(${setupOptions[db_name]} "magento_integration_tests" )
for db_name in "${db_names[@]}"; do
    mysql -e "drop database if exists ${db_name}; create database ${db_name};"
done

# Install Magento application
cd ${MAGENTO_ROOT}

install_cmd="./bin/magento setup:install \
    --db-host=${setupOptions[db_host]} \
    --db-name=${setupOptions[db_name]} \
    --db-user=${setupOptions[db_user]} \
    --backend-frontname=${setupOptions[admin_frontname]} \
    --base-url=${setupOptions[base_url]} \
    --language=${setupOptions[language]} \
    --timezone=${setupOptions[timezone]} \
    --currency=${setupOptions[currency]} \
    --admin-lastname=${setupOptions[admin_lastname]} \
    --admin-firstname=${setupOptions[admin_firstname]} \
    --admin-email=${setupOptions[admin_email]} \
    --admin-user=${setupOptions[admin_user]} \
    --admin-password=${setupOptions[admin_password]} \
    --cleanup-database \
    --use-rewrites=1"

# Configure Rabbit MQ
if [ -f "${MAGENTO_ROOT}/app/code/Magento/Amqp/registration.php" ]; then
    install_cmd="${install_cmd} \
    --amqp-host=${setupOptions[amqp_host]} \
    --amqp-port=${setupOptions[amqp_port]} \
    --amqp-user=${setupOptions[amqp_user]} \
    --amqp-password=${setupOptions[amqp_password]}"
fi

chmod +x bin/magento
php ${install_cmd}

# Enable Magento cron jobs
echo "* * * * * php ${MAGENTO_ROOT}/bin/magento cron:run &" | crontab -u vagrant -

if [ ${use_nfs_for_synced_folders} -eq 0 ]; then
    chown -R vagrant:vagrant ${MAGENTO_ROOT}
fi

# Generate XSD references for PHP Storm
php bin/magento dev:urn-catalog:generate /vagrant/.idea/misc.xml
sed -i "s|${MAGENTO_ROOT}|${MAGENTO_ROOT_HOST}|g" "/vagrant/.idea/misc.xml"

set +x
echo "
Magento application was deployed to ${MAGENTO_ROOT} and installed successfully
Access storefront at ${setupOptions[base_url]}
Access admin panel at ${setupOptions[base_url]}${setupOptions[admin_frontname]}/"

if [ ${use_nfs_for_synced_folders} -eq 0 ]; then
    echo "
    [Optional] To finish developer environment set up:
        1. Please create new PhpStorm project using 'magento2ce' directory on your host
        (this directory should already contain Magento repository cloned earlier)

        2. Use instructions provided here https://github.com/paliarush/vagrant-magento/blob/2.0/docs/phpstorm-configuration-windows-hosts.md
        to set up synchronization in PhpStorm (or using rsync) with ${MAGENTO_ROOT} directory"
fi
